/* 
 *       __        __           _______    _ 
 *      / / __    / /  __   __  \_  __/   |_|    ___     _____
 * 	   / / /  \  / /  / /  / /   / /     / /    / _ |   |  _  |  
 *    / /_/ /\ \/ /  / /__/ /   / /     / /    / __ |   / | | |
 *    \____/  \__/   \_____/   / /     /___\  /_/ | |  /_/  |_|
 *                      __    / /                 |/
 *                      \ \__/ /
 *                       \____/
 * 
 */

package org.wltea.analyzer.lucene;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.analysis.util.ResourceLoader;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
import org.apache.lucene.analysis.util.TokenFilterFactory;
import org.apache.lucene.util.Version;

import com.hunteron.jdbc.bean.DicKeyword;
import com.hunteron.jdbc.dao.DicKeywordDao;
import com.hunteron.util.DateFormatUtil;
public class SingleWordFilterFactory extends TokenFilterFactory implements
		ResourceLoaderAware, TimelyThread.UpdateJob {

	public SingleWordFilterFactory(Map<String, String> args) {
		super(args);
		if(!isStartThread) {
			synchronized (isStartThread) {
				if(!isStartThread) {
					isStartThread = true;
					try {
						update();
					} catch (IOException e) {
						e.printStackTrace();
					}
					TimelyThread.getInstance().register(this);
				}
			}
		}
	}

	public void inform(ResourceLoader loader) throws IOException {
	}

	@Override
	public TokenStream create(TokenStream arg0) {

		SingleWordFilter singleWordFilter = new SingleWordFilter(true, arg0, singleWord);

		return singleWordFilter;
	}
	private static Boolean isStartThread = false;

	private static CharArraySet singleWord = new CharArraySet(Version.LUCENE_47, 10, true);

	private static long dicUpdateTime = 0;
	@Override
	public void update() throws IOException {
		long minAddTime = dicUpdateTime;
		DicKeywordDao dicKeywordDao = new DicKeywordDao();
		dicKeywordDao.setUpdateTime(minAddTime);
		dicUpdateTime = dicKeywordDao.maxTime(minAddTime);
		
		if(dicUpdateTime == minAddTime) {
			dicUpdateTime = minAddTime + 1000;
		}
		
		System.out.println(DateFormatUtil.formatLong(System.currentTimeMillis()) + " : update single word filter");

		while(dicKeywordDao.hasNext()) {
			List<DicKeyword> list = dicKeywordDao.more();
			if(list != null) {
				for(DicKeyword k : list) {
					String w = k.getKeyword();
					if(w != null && w.length() == 1){
						singleWord.add(w);
					}
				}
			}
		}
		
	}
}

//
//class SingleWordUpdateThread implements Runnable {
//	public static boolean isStart = false;
//	public static SingleWordUpdateThread instance = null;
//	private SingleWordUpdateThread() {
//	}
//	public static SingleWordUpdateThread getInstance() {
//		if(instance == null) {
//			instance = new SingleWordUpdateThread();
//		}
//		return instance;
//	}
////	private final long updateTime = PropertieUtil.getLongProp("mongoDB",
////			"dicUpdateTime");
//	private final long reLoadTime = PropertieUtil.getLongProp("resource", "singleWordUpdateTime");
//
//	@Override
//	// 运行守护进程， 不断更新分词词库
//	public void run() {
//		
//		if(!isStart) {
//			isStart = true;
//			while (true) {
//				try {
//					Thread.sleep(reLoadTime);
//					update();
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		}
//		
//		
//	}
//}